using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace BassMati.Server
{
	/// <summary>
	/// Summary description for MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuStart;
		private System.Windows.Forms.MenuItem menuStop;
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			Trace.IndentSize = 2;
			InitializeComponent();
			this.server.ClientConnected += new BassMati.Server.Server.ClientConnectionEventHandler ( server_ClientConnected );
			this.server.ClientDisconnected += new BassMati.Server.Server.ClientConnectionEventHandler ( server_ClientDisconnected );
		}

		protected System.Diagnostics.TraceSwitch traceLevel = new TraceSwitch( "MainFormTraceLevel", "Controls trace information output level." );
		protected BassMati.Server.Server server = new Server( );
		protected string WindowTitleFormat = "Server ({0} clients connected)";

		#region - Generated Code -

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuStart = new System.Windows.Forms.MenuItem();
			this.menuStop = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuStart,
																					 this.menuStop});
			// 
			// menuStart
			// 
			this.menuStart.Index = 0;
			this.menuStart.Text = "St&art";
			this.menuStart.Click += new System.EventHandler(this.menuStart_Click);
			// 
			// menuStop
			// 
			this.menuStop.Index = 1;
			this.menuStop.Text = "St&op";
			this.menuStop.Click += new System.EventHandler(this.menuStop_Click);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.Tag = "";
			this.Text = ((string)(configurationAppSettings.GetValue("MainForm.Text", typeof(string))));

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
			System.Windows.Forms.Application.Run(new MainForm());
		}
		
		#endregion - Generated Code -

		private void menuStart_Click(object sender, System.EventArgs e)
		{
			Trace.WriteLineIf ( traceLevel.TraceInfo, "menuStart_Click() enter", "Info (Server.exe)" );
			Trace.Indent ( );
			try
			{
				this.server.Start( );
			}
			catch ( System.Exception ex )
			{
				MessageBox.Show( this, ex.Message, "Error (Server.exe)", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally
			{
				Trace.Unindent ( );
				Trace.WriteLineIf ( traceLevel.TraceInfo, "menuStart_Click() exit", "Info (Server.exe)" );
			}
		}

		private void menuStop_Click(object sender, System.EventArgs e)
		{
			Trace.WriteLineIf ( traceLevel.TraceInfo, "menuStop_Click() enter", "Info (Server.exe)" );
			Trace.Indent ( );
			try
			{
				this.server.Stop( );
			}
			catch ( System.Exception ex )
			{
				MessageBox.Show( this, ex.Message, "Error (Server.exe)", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally
			{
				Trace.Unindent ( );
				Trace.WriteLineIf ( traceLevel.TraceInfo, "menuStop_Click() exit", "Info (Server.exe)" );
			}
		}

		private void server_ClientConnected ( object sender, BassMati.Server.Server.ClientConnectionEventArgs e )
		{
			this.Text = string.Format( WindowTitleFormat , e.ClientsTotal );
		}

		private void server_ClientDisconnected ( object sender, BassMati.Server.Server.ClientConnectionEventArgs e )
		{
			this.Text = string.Format( WindowTitleFormat , e.ClientsTotal );
		}

		private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
		{
			System.Windows.Forms.MessageBox.Show( null, e.Exception.Message, "Thread Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop );
		}
	}
}
